//+------------------------------------------------------------------+
//|                             Short-Term Maximums And Minimums.mq4 |
//|                                  Copyright 2025, MetaQuotes Ltd. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2025, MetaQuotes Ltd."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
#property indicator_chart_window
//--- indicator settings
#property indicator_buffers 2
#property indicator_color1 Orange
#property indicator_color2 MediumTurquoise
#property indicator_width1 1
#property indicator_width2 1
//--- indicator parameters
input int Q = 200;        // Bars

//--- indicator buffers
double Buffer0[],Buffer1[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   IndicatorBuffers(2);
   IndicatorDigits(Digits);

   SetIndexBuffer(0,Buffer0);
   SetIndexStyle(0,DRAW_ARROW);
   SetIndexArrow(0,159);

   SetIndexBuffer(1,Buffer1);
   SetIndexStyle(1,DRAW_ARROW);
   SetIndexArrow(1,159);
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
  {
//----
   if(rates_total==prev_calculated)
      return(rates_total);

   int q=Q;
   int c=rates_total-2;
   if(q>c)
      q=c;
   int i;
   
//---- Short-Term Maximums
   for(i=1; i<q; i++)
     {
      Buffer0[i]=EMPTY_VALUE;
      Buffer1[i]=EMPTY_VALUE;
      //---- at right
      for(c=i-1; c>=0; c--)
        {
         if(high[i]>high[c])
           {
            if(low[i]>low[c])
              {
               c=-2;
               break;
              }
            else
              {
               if(c-1>-1 && high[i]>high[c-1] && (low[c]>low[c-1] || high[c]<high[c-1]))
                 {
                  c=-2;
                  break;
                 }
              }
           }
         else
           {
            if(high[i]<=high[c])
               break;
           }
        }
      //----
      if(c!=-2)
         continue;
      //---- at left
      for(c=i+1; c<rates_total; c++)
        {
         if(high[i]>high[c])
           {
            if(low[i]>low[c])
              {
               Buffer0[i]=high[i]+0.09*iATR(NULL,PERIOD_CURRENT,18,i);
               break;
              }
            else
              {
               if(c+1<rates_total && high[i]>high[c+1] && (low[c]>low[c+1] || high[c]<high[c+1]))
                 {
                  Buffer0[i]=high[i]+0.09*iATR(NULL,PERIOD_CURRENT,18,i);
                  break;
                 }
              }
           }
         else
           {
            if(high[i]<high[c])
               break;
           }
        }
     }
//---- Short-Term Minimums
   for(i=1; i<q; i++)
     {
      //--- at right
      for(c=i-1; c>=0; c--)
        {
         if(low[i]<low[c])
           {
            if(high[i]<high[c])
              {
               c=-2;
               break;
              }
            else
              {
               if(c-1>-1 && low[i]<low[c-1] && (high[c]<high[c-1] || low[c]>low[c-1]))
                 {
                  c=-2;
                  break;
                 }
              }
           }
         else
           {
            if(low[i]>=low[c])
               break;
           }
        }
      //---
      if(c!=-2)
         continue;
      //--- at left
      for(c=i+1; c<rates_total; c++)
        {
         if(low[i]<low[c])
           {
            if(high[i]<high[c])
              {
               Buffer1[i]=low[i]-0.09*iATR(NULL,PERIOD_CURRENT,18,i);
               break;
              }
            else
              {
               if(c+1<rates_total && low[i]<low[c+1] && (high[c]<high[c+1] || low[c]>low[c+1]))
                 {
                  Buffer1[i]=low[i]-0.09*iATR(NULL,PERIOD_CURRENT,18,i);
                  break;
                 }
              }
           }
         else
           {
            if(low[i]>low[c])
               break;
           }
        }
     }

//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+
